import { reqEmployee, addEmployee, updateEmployee,queryByEmployee, deleteByIds } from "@/api";

//search 小仓库
const state = {
    employeelist:[],
    
};
const getters= {};
const mutations ={
    //获取员工列表
    EMPLOYEELIST(state,data){ 
    state.employeelist = data;  
    },


    
};
const actions = {
    //获取员工列表
   async employeelist({commit}){
    //    发送请求
       let result = await reqEmployee();
       console.log(result)
       // 判断请求是否成功
         if(result.code==200){
          //    将数据封装到state中 commit调用mutations中的方法
          commit("EMPLOYEELIST",result.data);
         }
    },


    // 添加员工
    async addEmployee({ commit, dispatch }, data) {
        const response = await addEmployee(data)
        
        if (response.code !== 200) {
            //添加失败 返回错误信息
            
            return response.code
        }
        console.log(response.code)
        return response.code
    },
    async updateEmployee({ dispatch }, data) {
        const response = await updateEmployee(data)
        if (response.code !== 200) {
            //更新失败 返回错误信息
            return response.code
        }
        return response.code
    },

    //查询员工信息
    async queryByEmployee({commit},data){
        const result = await queryByEmployee(data)
        console.log(result)
        if(result.code === 200){
            commit("EMPLOYEELIST",result.data);
        }
    },

    //删除员工信息
     async deleteByIds({dispatch},data){
        const result = await deleteByIds(data)
        if(result.code === 200){
            dispatch('employeelist');
        }
        return result.code

     },
};
export default {
    state,
    getters,
    mutations,
    actions,
}